package default01;

import javax.swing.*;

/**
 * @TODO
 * @Author xu
 * 2025/3/14
 */
public class ExceptionTest {
    public String balance;
    private int a;
    private int b;
    private int c;

    public void inputNumbers() {
        try {
            // 输入两个数字
            a = Integer.parseInt(JOptionPane.showInputDialog("请输入第一个整数: "));
            b = Integer.parseInt(JOptionPane.showInputDialog("请输入第二个整数: "));

            // 进行四则运算
            c = a + b;
            JOptionPane.showMessageDialog(null, "加法结果: " + c);

            c = a - b;
            JOptionPane.showMessageDialog(null, "减法结果: " + c);

            c = a * b;
            JOptionPane.showMessageDialog(null, "乘法结果: " + c);

            // 处理除法，避免除以零的异常
            if (b == 0) {
                throw new ArithmeticException("除数不能为零");
            }
            c = a / b;
            JOptionPane.showMessageDialog(null, "除法结果: " + c);

        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "输入无效，请输入一个整数: " + e.getMessage());
        } catch (ArithmeticException e) {
            JOptionPane.showMessageDialog(null, "错误: " + e.getMessage());
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "发生了一个错误: " + e.getMessage());
        }
    }

    public static void main(String[] args) {
        ExceptionTest test = new ExceptionTest();
        test.inputNumbers();
    }

    public void withdraw(double amountToWithdraw) {
    }
}
